 using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Globalization;

namespace Postcode.WebControls
{
    public class PostcodeValidator : BaseValidator
    {
        private Postcode _postcode = null;
        private string _controlToMatch = null;
        private bool _strict = false;

        /// <summary>
        /// Get the postcode in a postcode format
        /// </summary>
        public Postcode Postcode
        {
            get
            {
                if (this._postcode == null)
                {
                    return new Postcode(this._currentControlToValidate.Text, this.getRegion());
                } // not yet set to create one
                return this._postcode;
            }
        }
        /// <summary>
        /// Do we want to match it agains a listcontrol to support multiple languages in 1 form
        /// </summary>
        public string ControlToMatch
        {
            get { return this._controlToMatch; }
            set { this._controlToMatch = value; }
        }
        /// <summary>
        /// Do we want strict matching or loose matching
        /// </summary>
        public bool Strict
        {
            get { return this._strict; }
            set { this._strict = value; }
        }

        private ITextControl _currentControlToValidate
        {
            get { return this.Page.FindControl(this.ControlToValidate) as ITextControl; }
        }
        private ListControl _currentControlToMatch
        {
            get 
            { 
                object obj = null;
                if (!String.IsNullOrEmpty(this.ControlToMatch))
                {
                    obj = this.Page.FindControl(this.ControlToMatch);
                }
                return (null != obj ) ? obj as ListControl : null; 
            }
        }

        /// <summary>
        /// Makes sure that both the ControlToValidate and ControlToMatch are present if needed
        /// </summary>
        /// <returns>Only true when everything is ok</returns>
        protected override bool ControlPropertiesValid()
        {
            if (this._currentControlToMatch != null)
            {   // if the currentControlToMatch != null add the js function to handle multiple different languages
                this.AddOnDataBoundEventHandler();
            }
            return (String.IsNullOrEmpty(this.ControlToMatch) || this._currentControlToMatch != null) && null != this._currentControlToValidate;
        }
        /// <summary>
        /// Returns whether the given postcode is valid according to the globalization region or ListControl region
        /// </summary>
        /// <returns></returns>
        protected override bool EvaluateIsValid()
        {
            this._postcode = new Postcode(this._currentControlToValidate.Text, this.getRegion());
            return this._postcode.IsValid(this._strict);
        }

        /// <summary>
        /// Get the culture we want to match agains
        /// </summary>
        /// <returns>The current culture or the culture as specified in the control to match</returns>
        private RegionInfo getRegion()
        {
            if (this._currentControlToMatch != null)
            {
                return new RegionInfo(this._currentControlToMatch.SelectedValue);
            }
            return RegionInfo.CurrentRegion;
        }
        
        /// <summary>
        /// An eventhandler that adds a js function that belongs to the current element which only validates languages
        /// that are in the dropdown list
        /// </summary>
        public void AddJs(object sender, EventArgs e)
        {
            
        }
        
        /// <summary>
        /// Adds the eventhandler that adds the needed js function for this dropdown
        /// </summary>
        private void AddOnDataBoundEventHandler()
        {
            ListControl lControl = this._currentControlToMatch;
            
            if (lControl != null)
            {
                lControl.DataBound += this.AddJs;
            }
        }
    }
}
